/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaeger.ninegridimgdemo.slice;

import com.bumptech.glide.Glide;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimgdemo.PostAdapter;
import com.jaeger.ninegridimgdemo.ResourceTable;
import com.jaeger.ninegridimgdemo.entity.Post;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayoutManager;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.jaeger.ninegridimageview.NineGridImageView.NOSPAN;
import static com.jaeger.ninegridimageview.NineGridImageView.TOPCOLSPAN;
import static com.jaeger.ninegridimageview.NineGridImageView.BOTTOMCOLSPAN;
import static com.jaeger.ninegridimageview.NineGridImageView.LEFTROWSPAN;

/**
 * 展示页面1
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String CONTENT = "看图，字不重要。想看大图？抱歉我还没做这个 ( •̀ .̫ •́ )";
    private List<Post> mPostList;
    private String[] IMG_URL_LIST = {
            "https://pic4.zhimg.com/02685b7a5f2d8cbf74e1fd1ae61d563b_xll.jpg",
            "https://pic4.zhimg.com/fc04224598878080115ba387846eabc3_xll.jpg",
            "https://pic3.zhimg.com/d1750bd47b514ad62af9497bbe5bb17e_xll.jpg",
            "https://pic4.zhimg.com/da52c865cb6a472c3624a78490d9a3b7_xll.jpg",
            "https://pic3.zhimg.com/0c149770fc2e16f4a89e6fc479272946_xll.jpg",
            "https://pic1.zhimg.com/76903410e4831571e19a10f39717988c_xll.png",
            "https://pic3.zhimg.com/33c6cf59163b3f17ca0c091a5c0d9272_xll.jpg",
            "https://pic4.zhimg.com/52e093cbf96fd0d027136baf9b5cdcb3_xll.png",
            "https://pic3.zhimg.com/f6dc1c1cecd7ba8f4c61c7c31847773e_xll.jpg",
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initEvent();
        initListContainer();
    }

    private void initListContainer() {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
        fummuyData();
        PostAdapter postAdapter = new PostAdapter(getContext(), mPostList, NineGridImageView.STYLE_FILL);
        listContainer.setItemProvider(postAdapter);
        listContainer.setLayoutManager(new DirectionalLayoutManager());
    }

    private void fummuyData() {
        mPostList = new ArrayList<>();
        // 单张
        mPostList.add(fummuyPost(1, NOSPAN));
        // 2张
        mPostList.add(fummuyPost(2, NOSPAN));

        // 3张,不跨行不跨列
        mPostList.add(fummuyPost(3, NOSPAN));

        // 3张,首行跨列
        mPostList.add(fummuyPost(3, TOPCOLSPAN));

        // 3张,末行跨列
        mPostList.add(fummuyPost(3, BOTTOMCOLSPAN));

        // 3张,首列跨行
        mPostList.add(fummuyPost(3, LEFTROWSPAN));

        // 4张,不跨行不跨列
        mPostList.add(fummuyPost(4, NOSPAN));

        // 4张,首行跨列
        mPostList.add(fummuyPost(4, TOPCOLSPAN));

        // 4张,末行跨列
        mPostList.add(fummuyPost(4, BOTTOMCOLSPAN));

        // 4张,首列跨行
        mPostList.add(fummuyPost(4, LEFTROWSPAN));

        // 5张,不跨行不跨列
        mPostList.add(fummuyPost(5, NOSPAN));

        // 5张,首行跨列
        mPostList.add(fummuyPost(5, TOPCOLSPAN));

        // 5张,末行跨列
        mPostList.add(fummuyPost(5, BOTTOMCOLSPAN));

        // 5张,首列跨行
        mPostList.add(fummuyPost(5, LEFTROWSPAN));

        // 6张,不跨行不跨列
        mPostList.add(fummuyPost(6, NOSPAN));

        // 6张,首行跨列
        mPostList.add(fummuyPost(6, TOPCOLSPAN));

        // 6张,末行跨列
        mPostList.add(fummuyPost(6, BOTTOMCOLSPAN));

        // 6张,首列跨行
        mPostList.add(fummuyPost(6, LEFTROWSPAN));
    }

    private Post fummuyPost(int num, int spanType) {
        List<String> imgUrls = new ArrayList<>();
        imgUrls.addAll(Arrays.asList(IMG_URL_LIST).subList(0, num % 9));
        return new Post(CONTENT, spanType, imgUrls);
    }

    private void initEvent() {
        findComponentById(ResourceTable.Id_btn_next).setClickedListener(
                listener -> {
                    present(new SecondAbilitySlice(), new Intent());
                }
        );
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
